import cv2
import matplotlib.pyplot as plt

img = cv2.imread('../images/card10.png')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 灰度图
ret1, thr1 = cv2.threshold(img, 99, 255, cv2.THRESH_BINARY)
# 小于阈值的像素点设置0，大于阈值的像素点设置为maxval
ret2, thr2 = cv2.threshold(img, 99, 255, cv2.THRESH_BINARY_INV)
# 小于阈值的像素点保持原色，大于阈值的像素点设置maxval
ret3, thr3 = cv2.threshold(img, 99, 255, cv2.THRESH_TRUNC)
# 小于阈值的像素点设置0，大于阈值的像素点保持原色
ret4, thr4 = cv2.threshold(img, 99, 255, cv2.THRESH_TOZERO)

titles = ['img', 'img_gray', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO']
images = [img, img_gray, thr1, thr2, thr3, thr4]

for i in range(6):
    plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')  # 2 行 3 列
    plt.title(titles[i])
plt.show()